package net.cloudcodex.client.character.handler;

import net.cloudcodex.client.CloudCodexEventBus;

import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBoxBase;

public class TextBoxBaseKeyHandler implements KeyDownHandler, KeyUpHandler {
	private String oldText = "";
	private boolean isOldTextdefine = false;
	private CloudCodexEventBus eventBus;

	public void setEventBus(CloudCodexEventBus eventBus) {
		this.eventBus = eventBus;
	}

	@Override
	public void onKeyDown(KeyDownEvent event) {
		TextBoxBase source = (TextBoxBase) event.getSource();
		if (!isOldTextdefine) {
			oldText = source.getText();
			isOldTextdefine = true;
		}
	}

	@Override
	public void onKeyUp(KeyUpEvent event) {
		TextBoxBase source = (TextBoxBase) event.getSource();
		if (!oldText.equals(source.getText())) {
			eventBus.updateButtonEnable(true, source.getTitle());
		} else {
			eventBus.updateButtonEnable(false, source.getTitle());
		}
	}

}